package com.example.dmc_android_sellbreakfast_ver1;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Database {

	Context mContext;
	SQLiteDatabase db;
	OpenHelper help;

	public Database(Context mContext) {
		super();
		this.mContext = mContext;
	}

	public Database open() {
		help = new OpenHelper(mContext);
		db = help.getWritableDatabase();
		return this;
	}

	public void close() {
		help.close();
	}

	public long Insert(String user, String pw, String name, String NumberRoom) {
		ContentValues values = new ContentValues();
		values.put(OpenHelper.COLUMN_USER, user);
		values.put(OpenHelper.COLUMN_PW, pw);
		values.put(OpenHelper.COLUMN_NAME, name);
		values.put(OpenHelper.COLUMN_ROOM, NumberRoom);
		return db.insert(OpenHelper.TB_NAME, null, values);
	}

	public boolean kiemTraLogin(String user, String pw) {
		Cursor c = db.rawQuery("select * from " + OpenHelper.TB_NAME
				+ " where " + OpenHelper.COLUMN_USER + " = ? and "
				+ OpenHelper.COLUMN_PW + " = ?", new String[] { user, pw });
		if (c.getCount() == 1) {
			c.close();
			return true;
		} else {
			c.close();
			return false;
		}
	}

	public String getName(String acc, String mk) {
		Cursor c = db.rawQuery("select * from " + OpenHelper.TB_NAME
				+ " where " + OpenHelper.COLUMN_USER + " = ? and "
				+ OpenHelper.COLUMN_PW + " = ?", new String[] { acc, mk });
		String data = "";
		if (c.getCount() == 1) {
			int i = c.getColumnIndex(OpenHelper.COLUMN_NAME);
			c.moveToFirst();
			data = c.getString(i);
			c.close();
			return data;
		} else {
			c.close();
			return "null data";
		}
	}
	public String getAccount(String acc, String mk) {
		Cursor c = db.rawQuery("select * from " + OpenHelper.TB_NAME
				+ " where " + OpenHelper.COLUMN_USER + " = ? and "
				+ OpenHelper.COLUMN_PW + " = ?", new String[] { acc, mk });
		String data = "";
		if (c.getCount() == 1) {
			int i = c.getColumnIndex(OpenHelper.COLUMN_USER);
			c.moveToFirst();
			data = c.getString(i);
			c.close();
			return data;
		} else {
			c.close();
			return "null data";
		}
	}
	public String getNumberRoom(String acc, String mk) {
		Cursor c = db.rawQuery("select * from " + OpenHelper.TB_NAME
				+ " where " + OpenHelper.COLUMN_USER + " = ? and "
				+ OpenHelper.COLUMN_PW + " = ?", new String[] { acc, mk });
		String data = "";
		if (c.getCount() == 1) {
			int i = c.getColumnIndex(OpenHelper.COLUMN_ROOM);
			c.moveToFirst();
			data = c.getString(i);
			c.close();
			return data;
		} else {
			c.close();
			return "null data";
		}
	}

	public ArrayList<Customer> getall() {
		ArrayList<Customer> kq = null;
		String sql = "SELECT * FROM " + OpenHelper.TB_NAME;
		Cursor c = db.rawQuery(sql, null);
		kq = new ArrayList<Customer>();
		Customer item;
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			int id = c.getColumnIndex(OpenHelper.COLUMN_ID);
			int name = c.getColumnIndex(OpenHelper.COLUMN_NAME);
			int user = c.getColumnIndex(OpenHelper.COLUMN_USER);
			int pw = c.getColumnIndex(OpenHelper.COLUMN_PW);
			int room = c.getColumnIndex(OpenHelper.COLUMN_ROOM);
			item = new Customer();
			item.setId(0);
			item.setUser(c.getString(user));
			item.setPw(c.getString(pw));
			item.setName(c.getString(name));
			item.setRoom(c.getString(room));
			kq.add(item);
		}
		return kq;
	}

}
